package domain;

import java.net.InetAddress;
import java.util.LinkedList;

import javax.swing.table.TableModel;

import com.audiobuddy.serializables.Song;
import com.audiobuddy.serializables.SongCommand;

import udp.UDPController;
import gui.GuiController;

public class ClientDomainController {

	private GuiController gc;
	private SongTableModel songTableModel;
	private UDPController udpController;
	private InetAddress serverIp;

	public void run() {
		gc = new GuiController(this);
		
		udpController = new UDPController(this);
		
		requestCurrentPlaylist();
		
	}

	public void requestCurrentPlaylist() {
		udpController.requestCurrentPlaylist();
		
	}

	public TableModel getSongTableModel() {
		if(songTableModel==null){
			songTableModel = new SongTableModel();
		}
		return songTableModel;
	}

	public void setSongs(LinkedList<Song> songs) {
		if(songTableModel==null){
			songTableModel = new SongTableModel();
		}
		songTableModel.setSongs(songs);
		
	}

	public void addSong(Song song) {
		udpController.addSong(song);
		
	}

	public void setServerIp(InetAddress address) {
		this.serverIp = address;
		
	}
	
	public InetAddress getServerIp(){
		if(serverIp==null){
			serverIp = udpController.getServerIp();
			gc.foundServerIp();
		}
		return serverIp;
	}

	public void playSong(int row) {
		udpController.stopBroadcasting();
		udpController.sendSongCommand(new SongCommand(SongCommand.PLAY, row));
		
	}

	public void streamSongToServer(String absolutePath) {
		udpController.stopBroadcasting();
		udpController.streamSongToServer(absolutePath);
		
	}

	public void stopSong() {
		udpController.sendSongCommand(new SongCommand(SongCommand.STOP, 0));
		udpController.stopBroadcasting();
	}

	public void stopBroadcasting() {
		udpController.stopLocalBroadcasting();
	}

	public void nextSong() {
		udpController.stopBroadcasting();
		udpController.sendSongCommand(new SongCommand(SongCommand.NEXT, 0));
		
	}

	public void previousSong() {
		udpController.stopBroadcasting();
		udpController.sendSongCommand(new SongCommand(SongCommand.PREVIOUS, 0));
		
	}

	public void showCurrentSong(String current) {
		gc.showCurrentSong(current);
		
	}

	public void delete(int row) {
		udpController.sendSongCommand(new SongCommand(SongCommand.DELETE, row));
		
	}

}
